package com.xieyong.mywebsite.mapper;

import com.xieyong.mywebsite.model.entity.BaseDict;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

@Mapper
public interface BaseDictMapper {

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict where is_deleted = 0 and id = #{id}")
    BaseDict selectOne(Integer id);

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict where is_deleted = 0 and id = #{id}")
    BaseDict selectOneByDictId(String dictId);

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict where is_deleted = 0 and dict_name like '%' + #{name} + '%'")
    BaseDict selectOneByDictName(String name);

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict")
    List<BaseDict> selectAll();

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict where is_deleted = 0 and dict_top_id = #{topId}")
    List<BaseDict> selectDictsByTopId(String topId);

    @Select("SELECT id,dict_top_id,dict_id,dict_name,is_active from base_dict where is_deleted = 0 and dict_id like #{preId} + '%'")
    List<BaseDict> selectDictsByPreID(String preId);

    List<BaseDict> selectByCondition(String topId, String preId, String name);

    @Insert("INSERT into base_dict (dict_top_id,dict_id,dict_name) values (#{dictTopId}, #{dictId}, #{dictName})")
    void insert(BaseDict BaseDict);

    @Update("UPDATE base_dict set dict_top_id = #{dictTopId}, dict_id = #{dictId}, dict_name = #{dictName} where id = #{id}")
    void update(BaseDict BaseDict);

    @Update("UPDATE base_dict set is_deleted = 1, deleted_time = #{deletedTime}, deleted_id = #{deletedId} where id = #{id}")
    void delete(Date deletedTime, Integer deletedId, Integer id);

    @Update("UPDATE base_dict set is_active = 1 - is_active where id = #{id}")
    void switchActive(Integer id);
}
